/**
 * @description 基础服务类
 * @author AI Assistant
 * @date 2023-10-27
 */

import { storage } from '@/utils/storage'

export class BaseService {
  constructor(storageKey) {
    this.storageKey = storageKey
  }

  getAll() {
    return storage.get(this.storageKey) || []
  }

  getById(id) {
    const items = this.getAll()
    return items.find(item => item.id === id)
  }

  create(data) {
    const items = this.getAll()
    const newItem = { ...data }
    items.push(newItem)
    storage.set(this.storageKey, items)
    return newItem
  }

  update(id, data) {
    const items = this.getAll()
    const index = items.findIndex(item => item.id === id)
    if (index > -1) {
      items[index] = { ...items[index], ...data, updateTime: Date.now() }
      storage.set(this.storageKey, items)
      return items[index]
    }
    return null
  }

  delete(id) {
    const items = this.getAll()
    const newItems = items.filter(item => item.id !== id)
    storage.set(this.storageKey, newItems)
    return true
  }
} 